<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/29 0029
 * Time: 22:04
 */

namespace console\controllers;

use app\models\ArcAndroid;
use app\models\ArcClassify;
use app\models\ArcProgress;
use app\models\ArcTrain;
use app\models\AsrAudio;
use app\models\AsrCustomer;
use app\models\AsrFiles;
use app\models\AsrKeyword;
use app\models\AsrKeywordClassify;
use app\models\AsrPlan;
use app\models\ThingRecordFile;
use common\helps\AsrSearch;
use common\helps\Foo;
use common\helps\ProcessStart;
use common\helps\Tools;

//打开所有级别的错误
//error_reporting(-1);

class DosomethingctiController extends CommonController
{
    static $aa;
    static $bb;
    static $process;
    /**
     * 后台跑拨打电话的流程
     * @param $id 为计划ID plan_id
     * @param $state 为状态 1是让CTI开始拨打电话，0为让CTI结束拨打电话
     */
    public function actionIndex($id, $state)
    {
        try{
            //数据的查询
            $asrSearchModel = new AsrSearch();
            $asrPlanModel = new AsrPlan();
            $asrPlan = $asrPlanModel->find()->where(['plan_id'=>$id])->one();
            //$asrAudio= $asrAudioModel->find()->where($asrPlan->audio_id)->one();
            //判断点击的按钮的状态  0 为打算拨打电话 1 为打算暂停电话
            if ($state == 0) {
                //等待CTI不再通话中
                //$this->waitCtiSleeping($asrPlan->audio_id,$id);
                //流程开始，更新状态字段为1
                $asrPlan->updateAttributes(['state'=>1]);
                //根据电话列表ID查找第一个开始电话
                $asrCustomer = AsrCustomer::find()->select('customer_id,phone_num,mobile_id,state')->orderBy("customer_id")->where('mobile_id = '.$asrPlan->mobile_id)->one();
                //根据audio_id 以及 start字段查找开场白
                $asrKeywordClassify = AsrKeywordClassify::find()->select('keyword_classify_id,file_id')->where(['audio_id'=>$asrPlan->audio_id,'start'=>1])->one();
                //拿到文件的ID
                $files_id = $asrKeywordClassify->file_id;
                //拿到文件的对象
                $asrFiles = AsrFiles::find()->select("filepath")->where("files_id=".$files_id)->one();
                //发送post数据到CTI端，
                $data["plan_id"] = $id;//计划ID
                $data["audio_id"] = $asrPlan->audio_id;//计划ID
                $data["call_state"] = 1;//开始打电话
                $data["customer_id"] = $asrCustomer->customer_id;//需要拨打出去的电话的ID
                $filepath = "E:/Recordfile/test2.wav";//需要播放的文件路径
                $phone_num = $asrCustomer->phone_num;
                $data["classify_type"] = 0;//节点的类型
                $data["classify_id"] = $asrKeywordClassify->keyword_classify_id;//本次流程的id

                Tools::l('http://192.168.2.247:8000/aitask?audio_id='.implode("_",$data).'&phone_num='.$phone_num.'&filepath='.$filepath.'&endhttp=http://192.168.2.119/cti-sleeping');
                $resObj = $asrSearchModel->actionSendpost('http://192.168.2.247:8000/aitask?audio_id='.implode("_",$data).'&phone_num='.$phone_num.'&filepath='.$filepath.'&endhttp=http://192.168.2.119/cti-sleeping',$data);
                //设置标志为正在通话
                $this->redis->set("asr_audio:".$asrPlan->audio_id.":asr_plan:plan_id=".$id.":cti_sleeping",1);
                //保存已经通话的结果
                $this->redis->set("asr_audio:".$asrPlan->audio_id.":asr_plan:plan_id=".$id.":from_cti",$resObj);
                //等待CTI不再通话中
                //$this->waitCtiSleeping($asrPlan->audio_id,$id);
            }else if($state == 1){//暂停
                //拿到CTI端是否正在打电话 $cti_sleeping == 1 为正在睡眠，0 为正在通话中
                //$this->waitCtiSleeping($asrPlan->audio_id,$id);
                //流程结束，更新状态字段为0
                $asrPlan->updateAttributes(['state'=>0]);
                //发送post数据到CTI端，
                $data["plan_id"] = $id;//计划ID
                $data["audio_id"] = $asrPlan->audio_id;//流程ID
                $data["from_php_cti_state"] = 3;//结束打电话的状态标识
                $data["customer_id"] = 0;//需要拨打出去的电话的ID
                $data["phone_num"] = 0;//电话号码为空
                $data["filepath"] = "E:/Recordfile/test2.wav";//结束打电话的状态标识
                $data["classify_type"] = 0;//节点的类型
                $data["classify_id"] = 0;//本次流程的id
                Tools::l('http://192.168.2.247:8000/aitask?audio_id='.$data["audio_id"].'&phone_num='.$data["phone_num"].'&trunk_num=&filepath='.$data["filepath"].'&endhttp=
http://192.168.2.119/cti-sleeping');
                $asrSearchModel->actionSendpost('http://192.168.2.247:8000/aitask?audio_id='.$data["audio_id"].'&phone_num='.$data["phone_num"].'&trunk_num=&filepath='.$data["filepath"].'&endhttp=
http://192.168.2.119/cti-sleeping',$data);
                //$asrSearchModel->actionSendpost('http://localhost/advanced/backend/web/asr-send/test',$data);
                //设置标志为正在通话
                $this->redis->set("asr_audio:".$asrPlan->audio_id.":asr_plan:plan_id=".$id.":cti_sleeping",0);
                $this->redis->set("asr_audio:".$asrPlan->audio_id.":asr_plan:plan_id=".$id.":cti_sleeping",0);
                //等待CTI不再通话中
                // $this->waitCtiSleeping($asrPlan->audio_id,$id);
            }
        }catch (\Exception $exception){
            Tools::l($exception);
            exit();
        }
    }


    /**
     * 等待CTI电话不在通话中
     */
    public function waitCtiSleeping($audio_id,$id){
        $cti_sleeping = $this->redis->get("asr_audio:".$audio_id.":asr_plan:plan_id=".$id.":cti_sleeping");
        if ($cti_sleeping == 1 || empty($cti_sleeping)){//表示正在睡眠，没有打电话
        }else if ($cti_sleeping == 0){
            //表示正在打电话，等待打电话完成就进行停止
            while($cti_sleeping == 0){
                //正在打电话就一直查找，redis，没有打电话了就往下走
                usleep(200);
                $cti_sleeping = $this->redis->get("asr_audio:".$audio_id.":asr_plan:plan_id=".$id.":cti_sleeping");
            }
        }
        return $cti_sleeping;
    }


    public function actionBack($a, $b)
    {
        if (substr(php_uname(), 0, 7) == "Windows"){
            exec("php ../../yii dosomethingcti/index " . $a . " " . $b );
        } else {
            exec("start /B php ../../yii dosomethingcti/index " . $a . " " . $b . " > /dev/null &");
        }
    }

}